<?php
// +--------------------------------------------------------------------------
// | ZAIYOUDAO [ 载攸道 先顺得常 ] <http://www.zaiyoudao.com>
// +--------------------------------------------------------------------------
// | Copyright © 2009-2016 载攸道 版权所有 <TEL:17090888854>
// +--------------------------------------------------------------------------
// | Project: YiPHP [ 我会的仅仅是偷懒！ ] <http://www.yiphp.com>
// +--------------------------------------------------------------------------
// | Author: 牛很多戒很多不戒 <n@isdce.com> <http://juexue.wang>
// +--------------------------------------------------------------------------

/**
 * 用户管理
 */
namespace Research\Controller;
use Common\Controller\AdminController;

/**
 * 用户管理控制器
 */
class DetailController extends AdminController{

	/*
	 * 用户管理
	 */
    public function Manage(){
		
		action_log('admin_user_manage');//行为记录
		
		common_public()->assign_page('调研管理');
		
		isFormAuth(I('param.id'),7);	
		
		
		$m_research_form = M('research_form');
		
		$m_research_conf = M('research_conf');
		
		$input_type_map['type'] = 3;
		$input_type_map['status'] = 1;
		$input_type_list = $m_research_conf->where($input_type_map)->order('value asc')->select();
		
		$input_type = I('param.input_type');
		
		$input_type = $input_type?$input_type:1;
		foreach($input_type_list as $ti=>$tk){
			
			if($input_type_list[$ti]['value']==$input_type){
				
				$input_type_list[$ti]['class'] = 'primary';
				
			}else{
				
				$input_type_list[$ti]['class'] = 'default';
			}
			
			
		}
		
		$this->assign('list',$list);
		$this->assign('input_type_list',$input_type_list);
		$this->assign('input_type',$input_type);
		
		$id = I('param.id');
		$this->assign('id',$id);
		
		$research_data_map['id'] = $id;
	
		$data = $m_research_form->where($research_data_map)->find();
		
		$this->assign('data',$data);
		
		$research_map['form_id'] = $id;
		$research_map['type'] = 2;
		$research_map['status'] = 1;
		$list = $m_research_form->where($research_map)->order('sort desc')->select();
		
		$this->assign('list',$list);
		
		$file_map['form_id'] = $id;
		$file_map['type'] = 2;
		$file_map['input_type'] = 6;
		$file_map['status'] = 1;
		$file_list = $m_research_form->where($file_map)->order('sort desc')->select();
		
		$this->assign('file_list',$file_list);
		
		
		
		
		$this->display();
	}
	 public function Test(){
		 
		dump(I());
		 die;
		 
	 }
	/*
	 * 新增调研
	 */
    public function Create(){
		
		common_public()->assign_page('新增调研');
		
		//isFormAuth(I('param.id'),5);
		
		if(IS_POST){
			
			$m_research_form = M('research_form');
			
			$data['input_type'] = I('param.input_type','','');
			$data['form_id'] = I('param.form_id','','');
			$data['name'] = I('param.name','','');
			$data['value'] = I('param.value','','');
			$data['sort'] = I('param.sort','','');
			$data['type'] = 2;
			
			
			
			$data['uid'] = getUID();
			$data['ip'] = get_client_ip();
			$data['time'] = time();
			$data['status'] = 1;
			
			$result = $m_research_form->add($data);
			
			
			if($result){
				$id = I('param.form_id','','');
				$input_type = I('param.input_type','','');
				$this->success('新增成功',U('Research/Detail/Manage',array('id'=>$id,'input_type'=>$input_type)));
				
			}else{
				
				$this->error('新增失败');
				
			}
			
		}
		
	}
	
	/*
	 * 修改用户
	 */
    public function Edit(){
		
		action_log('admin_user_manage');//行为记录
		
		common_public()->assign_page('调研管理');
		
		isFormAuth(I('param.id'),6);
		
		$m_research_form = M('research_form');
		$m_research_conf = M('research_conf');
		
		if(IS_POST){
			
			$m_research_form = M('research_form');
			
			$map['id'] = I('param.id','','');
			
			$data['name'] = I('param.name','','');
			$data['value'] = I('param.value','','');
			$data['sort'] = I('param.sort','','');

			
			$data['uid'] = getUID();
			$data['ip'] = get_client_ip();
			$data['time'] = time();
			$data['status'] = 1;
			
			$result = $m_research_form->where($map)->save($data);
			
			
			if($result){
				$id = I('param.form_id','','');
				$input_type = I('param.input_type','','');
				$this->success('新增成功',U('Research/Detail/Manage',array('id'=>$id,'input_type'=>$input_type)));
				
			}else{
				
				$this->error('新增失败');
				
			}
			
			
		}else{
			
			$input_type_map['type'] = 3;
			$input_type_map['status'] = 1;
			$input_type_list = $m_research_conf->where($input_type_map)->order('value asc')->select();
			
			$input_type = I('param.input_type');
			
			$input_type = $input_type?$input_type:1;
			foreach($input_type_list as $ti=>$tk){
				
				if($input_type_list[$ti]['value']==$input_type){
					
					$input_type_list[$ti]['class'] = 'primary';
					
				}else{
					
					$input_type_list[$ti]['class'] = 'default';
				}
				
				
			}
			
			$this->assign('list',$list);
			$this->assign('input_type_list',$input_type_list);
			$this->assign('input_type',$input_type);
			
			$id = I('param.id');
			$this->assign('id',$id);
			
			$research_data_map['id'] = $id;
		
			$data = $m_research_form->where($research_data_map)->find();
			
			$this->assign('data',$data);
			
			$research_map['form_id'] = $id;
			$research_map['type'] = 2;
			$research_map['status'] = 1;
			$list = $m_research_form->where($research_map)->order('sort desc')->select();
			
			$this->assign('list',$list);
			
			$file_map['form_id'] = $id;
			$file_map['type'] = 2;
			$file_map['input_type'] = 6;
			$file_map['status'] = 1;
			$file_list = $m_research_form->where($file_map)->order('sort desc')->select();
			
			$this->assign('file_list',$file_list);
			
			
			$input_id_map['id'] = I('param.input_id');
		
			$input_id_data = $m_research_form->where($input_id_map)->find();
			
			$this->assign('input_id_data',$input_id_data);
			
			
			$this->display();
			
		}
	}
	
	/*
	 * 编辑用户信息
	 */
	public function Status($id,$status=0){
		
		
		
		//判断是否是ajax请求
		if(IS_AJAX){
				
			$m_research_form = M('research_form');
			
			$id = $id?$id:I('param.id');
			$status = $status?$status:I('param.status');
			
			
			if($id){
				
			
				$id = explode(',',$id); 
				
				
				foreach($id as $i=>$k){

					$map['id'] = $id[$i];
					$rdata = $m_research_form->where($map)->find();
					
					isFormAuth($rdata['form_id'],8);
		
					$data['status'] = $status;
			
					$m_research_form->where($map)->save($data);
					
					
					
					
					
				}
				
				//返回信息
				$info = get_errcode(0);
			
			}else{
				
				$info = get_errcode(10000001);
			}
			
	
		
		}
		
		//返回信息		
		$info = $info?$info:get_errcode(-2);

		$this->ajaxReturn($info);


	}
	
	/*
	 * 管理用户群组
	 */
	public function Relevance(){
		
		//判断是否是ajax请求
		if(IS_AJAX){

			$m_common_user_group = M('common_user_group');
			$m_common_user_for_group = M('common_user_for_group');
			
			$type = I('param.type');
			
			$data['uid'] = I('param.uid');
			
			$data['gid'] = I('param.group');
			
			$data['status'] = 1;
			
			
			//是增加还是删除
			if($type==1){
				
				$gmap['id'] = $data['gid'];
				
				$gdata = $m_common_user_group->where($gmap)->find();
				
				//是不是内部组
				if($gdata['type']==1){
					
					//只能同时一个内部组
					$rmap['type'] = 1;
					$rmap['uid'] = $data['uid'];
					
					
					$m_common_user_for_group->where($rmap)->delete();
					
					$data['type'] = 1;
					$result = $m_common_user_for_group->add($data);
					
					
				}else if($gdata['type']==2){
					
					$data['type'] = 2;
					$result = $m_common_user_for_group->add($data);
				}
				
				
				
				
			}else if($type==2){
				
				$result = $m_common_user_for_group->where($data)->delete();
			}
			//获得数据
			
			
			
			
			//返回信息
			$info = $result?get_errcode(0):get_errcode(-1);
				
		}
		
		//返回信息		
		$info = $info?$info:get_errcode(-2);

		$this->ajaxReturn($info);
	}
	
	
}